<?php

/**
 * Description of Panel
 *
 * @author Ian Clarence <ian.clarence@gmail.com>
 */
class Panel extends ActiveRecord {

    /**
     * The table name.
     */
    const TABLE = "panel";

    /**
     * The articles which this panel contains.
     *
     * @var array
     */
    protected $articles = array();

    /**
     * The constructor.
     *
     * @param array $fields
     */
    public function __construct(array $fields = array()) {
        parent::__construct($fields);
        $this->fetchArticles();
    }

    public function fetchArticles() {
        $this->articles = PanelArticle::searchFor(self::TABLE, $this->getId(), "article");
    }

    public function getArticles() {
        return $this->articles;
    }

    /**
     * Getting panel parameters for passing into a view.
     *
     * @return array
     */
    public function getViewFields() {
        $viewFields = $this->getFields();

        /**
         * Articles.
         */
        $this->fetchArticles();
        $articles = $this->getArticles();
        $viewFields["articles"] = Article::getFieldsFromRecords($this->articles);
        foreach ($articles as $articleKey => $article) {
            if ($article instanceof Article) {
                $viewFields["articles"][$articleKey] = $article->getViewFields();
            }
        }
        
        return $viewFields;
    }

}